/*
 * p11464.cpp
 *
 *  Created on: 2013-5-30
 *      Author: zy
 */

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 20;
const int inf = 0x3f3f3f3f;
int n, A[maxn][maxn], B[maxn][maxn];

int check(int s)
{
	memset(B, 0, sizeof(B));
	for (int c = 0; c < n; c++)
	{
		if (s & (1 << c))
			B[0][c]=1;
		else if (A[0][c] == 1)
			return inf;
	}
	for (int r = 1; r < n; r++)
		for (int c = 0; c < n; c++)
		{
			int sum = 0;
			if (r > 1)
				sum += B[r - 2][c];
			if (c > 0)
				sum += B[r - 1][c - 1];
			if (c < n - 1)
				sum += B[r - 1][c + 1];
			B[r][c] = sum % 2;
			if (A[r][c] == 1 && B[r][c] == 0)
				return inf;
		}
	int cnt = 0;
	for (int r = 0; r < n; r++)
		for (int c = 0; c < n; c++)
			if (A[r][c] != B[r][c])
				cnt++;
	return cnt;
}

int main()
{
	int T;
	scanf("%d", &T);
	for (int cas = 1; cas <= T; cas++)
	{
		scanf("%d", &n);
		for (int r = 0; r < n; r++)
			for (int c = 0; c < n; c++)
				scanf("%d", &A[r][c]);
		int ans = inf;
		for (int s = 0; s < (1 << n); s++)
			ans = min(ans, check(s));
		if (ans == inf)
			ans = -1;
		printf("Case %d: %d\n", cas, ans);
	}
	return 0;
}
